<template>
	<view class="content">
		<view class="title">评价星级</view>
		<uni-rate :size="30" :value="apply_star_num" @change="changeStar"></uni-rate>
		<view class="title">评价内容</view>
		<textarea class="cont" placeholder-style="color:#CCCCCC" placeholder="请输入评价内容" maxlength="200" @input="descInput"></textarea >
		<view class="num">{{remnant}}/200</view>
		<view class="button" v-if="tid != ''" @tap="commit">提交评价</view>
		<view class="button" v-if="order_id != ''" @tap="submit">提交评价</view>
	</view>
</template>

<script>
	import uniRate from "@/components/uni-rate/uni-rate.vue"
	export default{
		data() {
			return {
				remnant: 0, //文本域内容的长度
				tid: '',
				apply_text: '', //评价内容
				apply_star_num: 1 ,//默认星级为一级
				order_id: ''
			}
		},
		components:{
			uniRate
		},
		onLoad(e) {
			this.tid = e.tid;
			this.order_id = e.order_id;
		},
		methods:{
			/* 获取评分 */
			changeStar(e) {
				this.apply_star_num = e.value;
			},
			/* 获取文本域的长度及内容 */
			descInput(e){
				this.remnant = e.detail.cursor;
				this.apply_text = e.detail.value;
				if(this.remnant >= 200) {
					uni.showToast({
						title: '最多只能输入200个字',
						icon: 'none'
					})
				}
			},
			commit() {
				this.$zapi.apiPost('user/resources.lists/GetResApplyByApplyid',{tid:this.tid}).then(res=>{
					if(res.data.code == 200) {
						if(res.data.data.is_apply == 'TRUE'){
							uni.showToast({
								title: '您已经评价过了，不能再次评价',
								icon: 'none'
							})
						} else {
							var data = {
								tid: this.tid,
								apply_text: this.apply_text,
								apply_star_num: this.apply_star_num
							}
							this.$zapi.apiPost('user/resources.lists/ResEvaluateAdd',data).then(res=>{
								uni.showToast({
									title: res.data.msg,
									icon: 'none'
								})
								if(res.data.code == 200) {
									setTimeout(function(){
										uni.navigateBack({
											delta: 1
										})
									},1000)
								}
							})
						}
					}
				})
			},
			submit() {
				var data = {
					order_id: this.order_id,
					content: this.apply_text,
					score: this.apply_star_num
				}
				this.$zbapi.apiPost('parent/order/comment/add',data).then(res=>{
					if(res.data.code == 0) {
						uni.showToast({
							title: '评价成功',
							icon: 'none'
						})
						setTimeout(function(){
							uni.navigateBack({
								delta: 1
							})
						},1000)
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style>
	.content{
		padding: 40upx;
	}
	.title{
		color: #666666;
		font-size: 34upx;
		line-height: 1;
		margin-bottom: 30upx;
	}
	.uni-rate{
		margin-bottom: 50upx;
	}
	.cont{
		color: #333333;
		font-size: 34upx;
		width: calc(100% - 60upx);
		background-color: #FAFAFA;
		padding: 30upx;
	}
	.num{
		margin-top: -50rpx;
		position: absolute;
		right: 60rpx;
		color: #CCCCCC;
	}
	.button{
		color: #FFFFFF;
		font-size: 34upx;
		background: #fe9900;
		text-align: center;
		height: 80upx;
		line-height: 80upx;
		position: fixed;
		bottom: 30upx;
		width: 690upx;
		margin: 0 auto;
	}
</style>
